package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Validated
@Slf4j
@RestController
@Api(tags = "02.车辆管理模块")
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    /**
     * 1.添加车辆
     */
    @PostMapping("add")
    @ApiOperation("添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, @ApiIgnore HttpSession session){
        /*
         * 1.接收数据[application/json];
         * 2.校验登录状态[session];
         *   2.1 未登录,直接返回;
         *   2.2 登录状态
         *       * 创建实体类
         *       * 复制属性
         *       * 设置其他属性的值
         *       * 调用接口插入数据
         *       * 返回响应
         */
        log.debug("vehicleAddParam = " + vehicleAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        //登录状态的校验已经在拦截器中实现
        //登录状态
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId()); //从userVO中获取用户id
        log.debug("vehicle = " + vehicle);
        vehicleMapper.insert(vehicle);

        return JsonResult.ok();
    }

    /**
     * 2.车辆列表
     */
    @GetMapping("list")
    @ApiOperation("车辆列表")
    @ApiOperationSupport(order = 20)
    public JsonResult vehicleList(VehicleListQuery vehicleListQuery, @ApiIgnore HttpSession session){
        log.debug("vehicleListQuery = " + vehicleListQuery);
        //校验用户登录状态
        UserVO userVO = (UserVO) session.getAttribute("user");
        //登录状态
        //SELECT ... FROM vehicle WHERE brand=? AND license=? AND user_id=?
        String brand = vehicleListQuery.getBrand();
        String license = vehicleListQuery.getLicense();
        Long uid = userVO.getId();
        List<VehicleListVO> vehicleListVOS = vehicleMapper.vehicleList(brand, license, uid);

        return JsonResult.ok(vehicleListVOS);
    }

    /**
     * 3.删除车辆
     */
    @PostMapping("del/{id}")
    @ApiOperation("删除车辆")
    @ApiOperationSupport(order = 30)
    public JsonResult deleteVehicle(@Range(min = 200, max = 300, message = "车辆编号必须在200-300之间") @PathVariable Integer id, @ApiIgnore HttpSession session){
        //手动抛出一个异常
        if (id < 0){
            throw new IllegalArgumentException("车辆编号不能小于0");
        }

        log.debug("id = " + id);
        UserVO userVO = (UserVO) session.getAttribute("user");
        //登录状态
        vehicleMapper.deleteVehicle(id, userVO.getId());

        return JsonResult.ok();
    }
}











